package com.ctshk.app.order.goods.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.web.support.BaseController;
import com.ctshk.rpc.order.goods.dto.CurrentUsreCartInfoDTO;
import com.ctshk.rpc.order.goods.req.ShoppingCartAddReq;
import com.ctshk.rpc.order.goods.service.IShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * TODO
 *
 * @author 黄利斌
 * @date 2021/5/20 5:05 下午
 */
@Api(value = "商城C端购物车", tags = "商城C端购物车")
@RestController
@RequestMapping("/order/commodity/cart")
public class OrderCommodityCartController extends BaseController {

    @DubboReference
    private IShoppingCartService iShoppingCartService;

    @ApiOperation(value = "獲取當前用戶購物車商品列表(已登录)", notes = "獲取當前用戶購物車商品列表(已登录)")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "獲取當前用戶購物車商品列表", operDesc = "獲取當前用戶購物車商品列表")
    @GetMapping("/findAppCurrentUsreCartInfoList/{code}/{from}/{size}")
    public Result<PageResponse<CurrentUsreCartInfoDTO>> findCurrentUsreCartInfoList(@PathVariable Integer code, @PathVariable Integer from, @PathVariable Integer size) throws Exception {
        if (null == from || null == size) {
            from = 1;
            size = 10;
        }
        IPage<CurrentUsreCartInfoDTO> page = new Page<>(from, size);
        List<CurrentUsreCartInfoDTO> cartInfoDTOs = iShoppingCartService.findPcCurrentUsreCartInfoList(getCurrentUser(getRequest()).getId(), code);
        if (CollectionUtils.isNotEmpty(cartInfoDTOs)) {
            PageResponse<CurrentUsreCartInfoDTO> result = new PageResponse<>(cartInfoDTOs, page.getCurrent() == 1, cartInfoDTOs.size() <= (page.getCurrent() * cartInfoDTOs.size()), cartInfoDTOs.size(), page.getCurrent(), cartInfoDTOs.size());
            return Result.success(result);
        }
        return Result.failed(SystemError.MALL_MERCHANDISE_23000);
    }

    @ApiOperation(value = "购物车商品增減購買數量(已登录)", notes = "购物车商品增減購買數量(已登录)")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "购物车商品增減購買數量", operDesc = "购物车商品增減購買數量")
    @PostMapping("/updateShoppingCartNumber")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "购物车商品信息，包含id(购物车id)、number(商品数量)两个字段",name = "req")
    })
    public Result updateShoppingCartNumber(@RequestBody @Valid JSONObject req) throws Exception {
        iShoppingCartService.updateShoppingCartNumber(req, getCurrentUser(getRequest()).getId());
        return Result.success();
    }

    @ApiOperation(value = "添加商品到购物车(已登录)", notes = "添加商品到购物车(已登录)")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "添加商品到购物车", operDesc = "添加商品到购物车")
    @PostMapping("/addPcShoppingCart")
    public Result addShoppingCart(@RequestBody @Valid ShoppingCartAddReq obj) throws Exception {
        iShoppingCartService.addPcShoppingCart(obj, getCurrentUser(getRequest()).getId());
        return Result.success();
    }

    @ApiOperation(value = "刪除购物车商品(已登录)", notes = "刪除购物车商品(已登录)")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.DELETE, operTitle = "刪除购物车商品", operDesc = "刪除购物车商品")
    @PostMapping("/delAppShoppingCart")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "购物车信息，包含ids(购物车id数组)：例如:[id1,id2]",name = "ids")
    })
    public Result delAppShoppingCart(@RequestBody @Valid List<Long> ids) throws Exception {
        iShoppingCartService.delAppShoppingCart(ids, getCurrentUser(getRequest()).getId());
        return Result.success();
    }

    @ApiOperation(value = "刪除购物车商品(未登录)", notes = "刪除购物车商品(未登录)")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.DELETE, operTitle = "刪除购物车商品", operDesc = "刪除购物车商品")
    @PostMapping("/delShoppingCartByLogout")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "购物车信息：id(商品id)、costId(属性id)、code(识别码)，例如：{\"shoppingCart\":[{id:\"1396764240739217409\",costId:\"1382572407269920769\"}],\"code\":\"90cf95a3-4e3c-463b-b51d-cecae31f711b\"}",name = "req")
    })
    public Result delShoppingCartByLogout(@RequestBody @Valid JSONObject req) throws Exception {
        iShoppingCartService.delShoppingCartByLogout(req);
        return Result.success();
    }

    @ApiOperation(value = "购物车商品增減購買數量(未登录)", notes = "购物车商品增減購買數量(未登录)")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.UPDATE, operTitle = "购物车商品增減購買數量", operDesc = "购物车商品增減購買數量")
    @PostMapping("/updateShoppingCartNumberByLogout")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "购物车商品信息，包含id(商品id)、number(商品数量)、code(识别码)、costId(属性id)四个字段",name = "req")
    })
    public Result updateShoppingCartNumberByLogout(@RequestBody @Valid JSONObject req) throws Exception {
        iShoppingCartService.updateShoppingCartNumberByLogout(req);
        return Result.success();
    }

    @ApiOperation(value = "购物车列表查询(未登录)", notes = "购物车列表查询(未登录)")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.QUERY, operTitle = "购物车列表查询", operDesc = "购物车列表查询")
    @GetMapping("/findUserShoppingCarInfoByLogout/{code}/{from}/{size}")
    public Result findUserShoppingCarInfoByLogout(@PathVariable String code, @PathVariable Integer from, @PathVariable Integer size) {
        return iShoppingCartService.findUserShoppingCarInfoByLogout(code, from, size);
    }

    @ApiOperation(value = "添加商品到购物车(未登录)", notes = "添加商品到购物车(未登录)")
    @OperationLogAnnotation(operModul = OperationLogModel.ORDER, operType = OperationLogType.ADD, operTitle = "添加商品到购物车(未登录)", operDesc = "添加商品到购物车(未登录)")
    @PostMapping("/addShoppingCartByLogout")
    public Result addShoppingCartByLogout(@RequestBody ShoppingCartAddReq addReq) {
        iShoppingCartService.addShoppingCartByLogout(addReq);
        return Result.success();
    }

}

